package com.micro.service.common.utils;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author lvxiucai
 * @description
 * @date 2019/8/28
 */
public class BCryptUtils {

    private final static BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

    /**
     * @author lvxiucai
     * @description BCrypt加密
     * @date 2019/8/28
     * @param
     * @return
     **/
    public static String encode(String str){
        if(StringUtils.isBlank(str)){
            return null;
        }
        return bCryptPasswordEncoder.encode(str);
    }

    /**
     * @author lvxiucai
     * @description 校验密码
     * @date 2019/8/28
     * @param
     * @return
     **/
    public static boolean match(String rawPass,String encodedPassword){
        return bCryptPasswordEncoder.matches(rawPass,encodedPassword);
    }

    public static void main(String[] args) {
        String rawPass = "qlyd-app";
        String enocdedPassword = bCryptPasswordEncoder.encode(rawPass);
        System.out.println(enocdedPassword);
        System.out.println(bCryptPasswordEncoder.matches(rawPass,enocdedPassword));
    }

}
